<?php

global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	if($_RQ['id']){
		$this->session('workerid',$_RQ['id']);
	}
	$workerid=$this->session('workerid');
	$worker=pdo_get('jy_yiliao_worker',array('id'=>$workerid,'uniacid'=>$_SESSION['uniacid']));
	if(!$worker){
		exi('请选择大夫！','error');
	}
		$currentyear = date('Y');
		$currentmonth = date('m');
		$years = array();
		$i = 0;
		while ($i <= 10) {
			$years[] = $currentyear + $i;
			++$i;
		}
		$months = array('01','02','03','04','05','06','07','08','09','10','11','12');
		$return['currentyear']=$currentyear;
		$return['currentmonth']=$currentmonth;
		$return['months']=$months;
		$return['years']=$years;	
		$year = trim($_RQ['year']);
		$month = trim($_RQ['month']);
		if(!$year){
			$year=$currentyear;
		}
		if(!$month){
			$month=$currentmonth;
		}
		$return['calendar'] =$return['kongday']= array();		
		$return['worker'] = $worker;
		$return['on_days']=array('0'=>'周日','1'=>'周一','2'=>'周二','3'=>'周三','4'=>'周四','5'=>'周五','6'=>'周六');
		exi($return);
}
if($_RQ['op']=='dates'){
	$workerid=$this->session('workerid');
		$year = trim($_RQ['year']);
		$month = trim($_RQ['month']);
		$day = date("t",strtotime($year.'-'.$month));
		//$day = cal_days_in_month(CAL_GREGORIAN, $month,$year);
		$startime=strtotime($year.'-'.$month);
		$endtime=strtotime($year.'-'.$month.'-'.$day);
		$nowtime=array($startime,$endtime);
		$calendar=pdo_getall('jy_yiliao_lesson_pai',array('workerid'=>$workerid,'ontime between'=>$nowtime,'uniacid'=>$_SESSION['uniacid']),array('storeid','time_hour','onday'),'onday');
		if (empty($calendar)) {
			$calendar = array();
			$i = 1;
			while ($i <= $day) {
				if ($i < 10) {
					$i = '0' . $i;
				}
				$calendar[date($year . '-' . $month . '-' . $i)] = false;
				++$i;
			}
		}else {
			$result = array();
			$i = 1;
			while ($i <= $day) {
				if ($i < 10) {
					$i = '0' . $i;
				}
				$date = $year . '-' . $month . '-' . $i;
				$result[$date] = false;
				if (isset($calendar[$date])) {
					$value = $calendar[$date];
					$result[$date] = false;
					if ($value){
						$title=pdo_getcolumn('jy_yiliao_store', array('id'=>$value['storeid']),'title');
						$result[$date] = array(
							'storeid' => $value['storeid'], 
							'title' =>$title,
							'time_hour'=>json_decode($value['time_hour'],true)
						);
					}
				}
				++$i;
			}
			$calendar = $result;
		}
		$d=array();
		foreach($calendar as $k=>$v){
			$d[$k]=date('d', strtotime($k));
		}
		$return['calendar'] = $calendar;
		$return['d'] = $d;
		$weekday=$year . '-' . $month . '-01';
		$return['week'] = date('w', strtotime($weekday));
		$return['year']=$year;
		$return['month']=$month;
		$return['kongday']=array();
		for($i=0;$i<$return['week'];$i++){
			$return['kongday'][]=$i;
		}
		exi($return);
}
if($_RQ['op']=='set'){
	if($_RQ['postdata']){
		$postdata=get_postdata();
		$storeid = $postdata['storeid'];
		$onday = $postdata['onday'];
		if (empty($storeid)){
			exi('请选择门店！','error');
		}
		if (empty($onday)){
			exi('请选择日期！','error');
		}
		//判断选择时间是否小于当前时间
		$onday2 = strtotime($onday);
		$today = strtotime(date('Y-m-d'));
		if($onday2<$today){
		    exi('选择判断需要大于当前日期','error');
		}
		$year = date('Y', $onday2);
		$month = date('m', $onday2);
		$store = pdo_get('jy_yiliao_store',array('id'=>$storeid),array('id','title','area_id'));
		if (empty($store)) {
			exi('门店未找到','error');
		}
		$data=array_fields(array('ontime','time_hour','lessonid','area_id','workerid','storeid','storetype','paixu','status','person_limit','isphone'),$postdata);
		$id=intval($postdata['id']);
		$data['ontime']=strtotime($onday);
		$data['time_hour']=ijson_encode($data['time_hour']);
		$data['onday']=$onday;
		//area_id--lessonid
		$data['area_id'] = $store['area_id'];
		$workers = pdo_get('jy_yiliao_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$this->session('workerid')));
		$data['lessonid'] = $workers['lesson_id'];
	    //groupid
		if($id){
			pdo_update('jy_yiliao_lesson_pai',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}else{
			$data['uniacid']=$_SESSION['uniacid'];
			$data['workerid']=$this->session('workerid');
			$data['status']=1;
			pdo_insert('jy_yiliao_lesson_pai',$data);
		}
		exi('设置成功','','/merch.lesson_pai/');
	}
	$onday=$_RQ['onday'];
	if (empty($onday)){
		exi('请选择日期！'.$onday,'error');
	}
	$item=pdo_get('jy_yiliao_lesson_pai',array('onday'=>$onday,'workerid'=>$this->session('workerid'),'uniacid'=>$_SESSION['uniacid']));
	$item['time_hour']=json_decode($item['time_hour'],true);
	$item['onday']=$onday;
	$htmls=array(
		'type'=>'edit',
		'edit'=>array(
			array('type'=>'time_hour','name'=>'time_hour','title'=>'上班时间段'),
			array('type'=>'select','options'=>'stores','name'=>'storeid','title'=>'挂诊医院'),
			array('type'=>'string','name'=>'person_limit','title'=>'总上限人数'),
			array('type'=>'switch','title'=>'是否电话预约','name'=>'isphone','help'=>''),
		)
	);
	$return['htmls']=$htmls;
	$return['item']=$item;
	$return['stores']=pdo_getall('jy_yiliao_store',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid),array('id','title'),'id');
	exi($return);
}
if($_RQ['op']=='delete'){
		$postdata=get_postdata();
		$date = trim($postdata['date']);
		if (empty($date)) {
			exi('参数错误，请选择日期！'.$date,'error');
		}
		pdo_delete('jy_yiliao_lesson_pai',array('onday'=>$date,'workerid'=>$this->session('workerid'),'uniacid'=>$_SESSION['uniacid']));
		exi('删除成功') ;
}
if($_RQ['op']=='clear'){
		$postdata=get_postdata();
		$year = trim($postdata['year']);
		$month = trim($postdata['month']);
		$onday=$year.'-'.$month;
		pdo_delete('jy_yiliao_lesson_pai',array('onday like'=>'%'.$onday.'%','workerid'=>$this->session('workerid'),'uniacid'=>$_SESSION['uniacid']));
		exi('清除成功') ;
}
if($_RQ['op']=='batch_set'){
	if($_RQ['postdata']){
		$postdata=get_postdata();
		$storeid = $postdata['storeid'];
		if (empty($storeid)){
			exi('请选择门店！','error');
		}
		$year = $postdata['year'];
		$month = $postdata['month'];
		$day = $this->model('lesson')->getcalendar($month,$year);
		if(!$day){
			exi('请先安装Calendar扩展，获取日历信息！','error');
		}
		$store = pdo_get('jy_yiliao_store',array('id'=>$storeid),array('id','title'));
		if (empty($store)) {
			exi('门店未找到','error');
		}
		$worker=pdo_get('jy_yiliao_worker',array('id'=>$this->session('workerid')));
		$onday=$year.'-'.$month;
		pdo_delete('jy_yiliao_lesson_pai',array('onday like'=>'%'.$onday.'%','workerid'=>$this->session('workerid'),'uniacid'=>$_SESSION['uniacid']));
		$data=array_fields(array('ontime','time_hour','lessonid','area_id','workerid','storeid','storetype','person_limit','paixu','status'),$postdata);
		$data['time_hour']=ijson_encode($data['time_hour']);
		unset($data['id']);
		$i = 1;
		while ($i <= $day){
					if ($i < 10) {
						$i = '0' . $i;
					}
					$daydata=$year.'-'.$month.'-'.$i;
					$weekid=date("w",strtotime($daydata));
					if(strpos($worker['on_days'],$weekid) !== false){
						$data['onday']=$daydata;
						$data['ontime']=strtotime($daydata);
						$data['uniacid']=$_SESSION['uniacid'];
						$data['workerid']=$this->session('workerid');
						pdo_insert('jy_yiliao_lesson_pai',$data);
					}else{
					}
					++$i;
		}
		exi('设置成功','','/merch.lesson_pai/');
	}
	$item=pdo_get('jy_yiliao_lesson_pai',array('workerid'=>$this->session('workerid'),'uniacid'=>$_SESSION['uniacid']));
	$item['year']=$_RQ['year'];
	$item['month']=$_RQ['month'];
	$htmls=array(
		'type'=>'edit',
		'edit'=>array(
			array('type'=>'time_hour','name'=>'time_hour','title'=>'上班时间段'),
			array('type'=>'select','options'=>'stores','name'=>'storeid','title'=>'挂诊医院'),
			array('type'=>'string','name'=>'person_limit','title'=>'总上限人数'),
		)
	);
	$return['htmls']=$htmls;
	$return['item']=$item;
	$return['stores']=pdo_getall('jy_yiliao_store',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid),array('id','title'),'id');
	exi($return);
}
if($_RQ['op']=='batch_delete'){
		$year = trim($_RQ['year']);
		$month = trim($_RQ['month']);
		if ($month < 10) {
			$month = '0' . $month;
		}
		$days = $_RQ['days'];
		if (empty($year) || empty($month)) {
			exi('参数错误','error');
		}
		if (!(is_array($days)) || empty($days)) {
			exi('参数错误','error');
		}
		if ($days[0] == 'all') {
			array_shift($days);
		}
		$redis = $this->model('model')->redis();
		$redis_prefix = $this->model('model')->get_prefix();
		$calendar = $redis->hGetAll($redis_prefix . 'calendar_' . $year . '_' . $month);

		if (!(is_array($calendar))) {
			$calendar = array();
		}
		$maxday = get_last_day($year, $month);
		$dates = array();
		$i = 1;
		while ($i <= $maxday) {
			if ($i < 10) {
				$i = '0' . $i;
			}
			$date = date($year . '-' . $month . '-' . $i);
			$week = date('w', strtotime($date));

			if ($week == 0) {
				$week = 7;
			}
			if (in_array($week, $days)) {
				if (is_array($calendar) && isset($calendar[$date])) {
					unset($calendar[$date]);
					$redis->hDel($redis_prefix . 'calendar_' . $year . '_' . $month, $date);
					$dates[] = $date;
				}

			}
			++$i;
		}

		if (empty($calendar)) {
			$redis->delete($redis_prefix . 'calendar_' . $year . '_' . $month);
		}
		exi(array('dates' => implode(',', $dates)));
	}
?>
